package com.hzjh.service.videolib

import android.content.res.Configuration
import android.os.Bundle
import android.view.KeyEvent
import androidx.viewbinding.ViewBinding
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.BarUtils
import com.hzjh.base.ext.frame.JHBaseActivity
import com.hzjh.base.ext.frame.JHBaseVM
import com.hzjh.base.log.JHLogger
import com.hzjh.base.utils.other.ActivityStackManage
import com.hzjh.base.utils.other.CommUtils
import com.hzjh.base.utils.other.JHAppStateManager
import com.hzjh.service.videolib.widget.SampleCoreVideo
import com.shuyu.gsyvideoplayer.GSYVideoManager
import com.shuyu.gsyvideoplayer.builder.GSYVideoOptionBuilder
import com.shuyu.gsyvideoplayer.listener.VideoAllCallBack
import com.shuyu.gsyvideoplayer.utils.OrientationUtils

abstract class BaseVideoActivity<T : ViewBinding, VM : JHBaseVM> : JHBaseActivity<T, VM>(),
    VideoAllCallBack {
    private val TAG = "BaseVideoActivity"

    private var isPlay = false
    private var isPause = false
    private val orientationUtils by lazy { OrientationUtils(this, getGSYVideoPlayer()) }

    override fun onCreate(savedInstanceState: Bundle?) {
        //改主题
        BarUtils.setStatusBarLightMode(this, isStatusBarLightMode())
        BarUtils.transparentStatusBar(this)
        ARouter.getInstance().inject(this)
        super.onCreate(savedInstanceState)
        ActivityStackManage.addActivity(this)
    }

    // true: black icon / false: white icon
    override fun isStatusBarLightMode() = true

    open fun initVideo() {
        orientationUtils.isEnable = false
        if (getGSYVideoPlayer().fullscreenButton != null) {
            getGSYVideoPlayer().fullscreenButton.setOnClickListener {
                showFull()
                clickForFullScreen()
            }
        }
        //视频布局文件 R.layout.video_layout_standard
    }

    open fun playVideo() {
        getGSYVideoPlayer().startPlayLogic()
    }

    open fun initVideoBuilderMode() {
        initVideo()
        getGSYVideoOptionBuilder().setVideoAllCallBack(this).build(getGSYVideoPlayer())
    }

    open fun showFull() {
        if (orientationUtils.isLand != 1) {
            orientationUtils.resolveByClick()
        }
        getGSYVideoPlayer().startWindowFullscreen(
            this, hideActionBarWhenFull(),
            hideStatusBarWhenFull()
        )
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            orientationUtils.backToProtVideo()
            if (!GSYVideoManager.backFromWindowFull(this)) {
                return super.onKeyDown(keyCode, event)
            }
            return true
        }
        return super.onKeyDown(keyCode, event)
    }

    override fun onPause() {
        JHLogger.e(TAG, "video onPause")
        super.onPause()
        getGSYVideoPlayer().currentPlayer.onVideoPause()
        isPause = true
    }

    override fun onRestart() {
        JHLogger.e(TAG, "video onRestart")
        super.onRestart()
        playVideo()
    }

    override fun onResume() {
        JHLogger.e(TAG, "video onResume")
        super.onResume()
        getGSYVideoPlayer().currentPlayer.onVideoResume()
        isPause = false
    }

    override fun onDestroy() {
        JHLogger.e(TAG, "video onDestroy")
        if (isPlay) {
            getGSYVideoPlayer().currentPlayer.release()
        }
        orientationUtils.releaseListener()
        ActivityStackManage.removeActivityStack(this)
        super.onDestroy()
    }

    override fun onConfigurationChanged(newConfig: Configuration) {
        super.onConfigurationChanged(newConfig)
        if (isPlay && !isPause) {
            getGSYVideoPlayer().onConfigurationChanged(
                this, newConfig,
                orientationUtils, hideActionBarWhenFull(), hideStatusBarWhenFull()
            )
        }
    }

    override fun onStartPrepared(url: String?, vararg objects: Any?) {
        JHLogger.e(TAG, "video onStartPrepared")
    }

    override fun onPrepared(url: String?, vararg objects: Any?) {
        JHLogger.e(TAG, "video onPrepared")
        orientationUtils.isEnable = getDetailOrientationRotateAuto() && !isAutoFullWithSize()
        isPlay = true

        CommUtils.getHandler().postDelayed({
           if(JHAppStateManager.isAppTurnIntoBackGround()){
               //在后台运行时不播放
               getGSYVideoPlayer().currentPlayer.onVideoPause()
               isPause = true
           }
        },100)
    }

    override fun onClickStartIcon(url: String?, vararg objects: Any?) {
        JHLogger.e(TAG, "video onClickStartIcon")
    }

    override fun onClickStartError(url: String?, vararg objects: Any?) {
        JHLogger.e(TAG, "video onClickStartError")
    }

    override fun onClickStop(url: String?, vararg objects: Any?) {
        JHLogger.e(TAG, "video onClickStop")
    }

    override fun onClickStopFullscreen(url: String?, vararg objects: Any?) {
        JHLogger.e(TAG, "video onClickStopFullscreen")
    }

    override fun onClickResume(url: String?, vararg objects: Any?) {
        JHLogger.e(TAG, "video onClickResume")
    }

    override fun onClickResumeFullscreen(url: String?, vararg objects: Any?) {
        JHLogger.e(TAG, "video onClickResumeFullscreen")
    }

    override fun onClickSeekbar(url: String?, vararg objects: Any?) {}

    override fun onClickSeekbarFullscreen(url: String?, vararg objects: Any?) {}

    override fun onAutoComplete(url: String?, vararg objects: Any?) {
        JHLogger.e(TAG, "video onAutoComplete")
    }

    override fun onEnterFullscreen(url: String?, vararg objects: Any?) {}

    override fun onQuitFullscreen(url: String?, vararg objects: Any?) {
        orientationUtils.backToProtVideo()
    }

    override fun onQuitSmallWidget(url: String?, vararg objects: Any?) {}

    override fun onEnterSmallWidget(url: String?, vararg objects: Any?) {}

    override fun onTouchScreenSeekVolume(url: String?, vararg objects: Any?) {
        JHLogger.e(TAG, "video onTouchScreenSeekVolume")
    }

    override fun onTouchScreenSeekPosition(url: String?, vararg objects: Any?) {
        JHLogger.e(TAG, "video onTouchScreenSeekPosition")
    }

    override fun onTouchScreenSeekLight(url: String?, vararg objects: Any?) {
        JHLogger.e(TAG, "video onTouchScreenSeekLight")
    }

    override fun onPlayError(url: String?, vararg objects: Any?) {
        JHLogger.e(TAG, "video onPlayError")
    }

    override fun onClickStartThumb(url: String?, vararg objects: Any?) {
        JHLogger.e(TAG, "video onClickStartThumb")
    }

    override fun onClickBlank(url: String?, vararg objects: Any?) {
        JHLogger.e(TAG, "video onClickBlank")
    }

    override fun onClickBlankFullscreen(url: String?, vararg objects: Any?) {
        JHLogger.e(TAG, "video onClickBlankFullscreen")
    }

    open fun hideActionBarWhenFull(): Boolean {
        return true
    }

    open fun hideStatusBarWhenFull(): Boolean {
        return true
    }

    abstract fun getGSYVideoPlayer(): SampleCoreVideo
    abstract fun getGSYVideoOptionBuilder(): GSYVideoOptionBuilder

    abstract fun clickForFullScreen()

    abstract fun getDetailOrientationRotateAuto(): Boolean

    open fun isAutoFullWithSize(): Boolean {
        return false
    }

    override fun onComplete(url: String?, vararg objects: Any?) {}
}